package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.SetMealService;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Api(tags = "购物车")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private DishService dishService;


    @PostMapping("/add")
    @ApiOperation(value = "购物车添加菜品")
    public Result addShopCart(@RequestBody ShoppingCartDTO shoppingCartDTO){

        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
//cart == 传过来的 菜/套餐 在库中有没有
        ShoppingCart cart = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId,userId)
                .eq(shoppingCartDTO.getDishId() != null,ShoppingCart::getDishId,shoppingCartDTO.getDishId())
                .eq(shoppingCartDTO.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCartDTO.getSetmealId())
                .eq(shoppingCartDTO.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCartDTO.getDishFlavor())
                .one();

        if(cart == null) {
            if(shoppingCart.getSetmealId() != null){
                Setmeal setmeal = setMealService.getById(shoppingCart.getSetmealId());

                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());

            }else {

                Dish dish = dishService.getById(shoppingCart.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(1);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
            ShoppingCart cartNotNull = cart;
            cartNotNull.setNumber(cartNotNull.getNumber() + 1);
            shoppingCartService.updateById(cartNotNull);
        }


        return Result.success();
    }

    @ApiOperation(value = "查询购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        Long id = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, id)
                .list();

        return Result.success(list);

    }

    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public Result clean(){
        Long id = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, id)
                .list();
        shoppingCartService.removeBatchByIds(list);
        return Result.success();
    }

    @PostMapping("/sub")
    @ApiOperation(value = "减去一个商品")
    public Result sub(ShoppingCartDTO shoppingCartDTO){
        Long id = BaseContext.getCurrentId();

            ShoppingCart cart = shoppingCartService.lambdaQuery()
                    .eq(ShoppingCart::getUserId, id)
                    .eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId())
                    .eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId())
                    .eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor())
                    .one();

            cart.setNumber(cart.getNumber() - 1);
            if(cart.getNumber() == 0){
                shoppingCartService.removeById(cart.getId());
            }
            shoppingCartService.updateById(cart);
        return Result.success();
    }
}
